package cn.wetcland.urban.admin.service.impl;

import cn.wetcland.urban.admin.dto.SwiperAddDTO;
import cn.wetcland.urban.admin.dto.SwiperUpdateDTO;
import cn.wetcland.urban.admin.mapper.UtArticleMapper;
import cn.wetcland.urban.admin.mapper.UtFileUploadMapper;
import cn.wetcland.urban.admin.mapper.UtSwiperMapper;
import cn.wetcland.urban.admin.service.UtSwiperService;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.generator.domain.UtArticle;
import cn.wetcland.urban.generator.domain.UtFileUpload;
import cn.wetcland.urban.generator.domain.UtSwiper;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author hongyao.tan
* @description 针对表【ut_swiper】的数据库操作Service实现
* @createDate 2024-03-07 17:17:42
*/
@Service
public class UtSwiperServiceImpl extends ServiceImpl<UtSwiperMapper, UtSwiper> implements UtSwiperService {

    @Resource
    public UtSwiperMapper swiperMapper;

    @Resource
    public UtFileUploadMapper fileUploadMapper;

    @Resource
    public UtArticleMapper articleMapper;

    @Override
    public Page<UtSwiper> swiperList(BaseConditionSearchDTO searchDTO) {
        Page<UtSwiper> utSwiperPage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return swiperMapper.selectSwiperPageList(utSwiperPage, searchDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void swiperUpdate(SwiperUpdateDTO updateDTO) {
        UtSwiper utSwiper = swiperMapper.selectById(updateDTO.getSwiperId());
        MyAssert.notNull(utSwiper, "该轮播图不存在");
        CopyObjectUtil.copyPropertiesIgnoreNull(updateDTO, utSwiper);
        swiperMapper.updateById(utSwiper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void swiperDelete(Long swiperId) {
        swiperMapper.deleteById(swiperId);
    }

    @Override
    public void swiperAdd(SwiperAddDTO addDTO) {
        UtFileUpload utFileUpload = fileUploadMapper.selectById(addDTO.getSwiperImgId());
        MyAssert.notNull(utFileUpload, "该图片不存在");
        UtArticle utArticle = articleMapper.selectById(addDTO.getRelArticleId());
        MyAssert.notNull(utArticle, "关联文章不存在");
        UtSwiper utSwiper = new UtSwiper();
        CopyObjectUtil.copyPropertiesIgnoreNull(addDTO, utSwiper);
        swiperMapper.insert(utSwiper);
    }
}




